<?php

namespace App\Http\Controllers\System;


use App\Events\EmailEvent;
use App\Model\System\MailConfig;
use App\Model\System\SystemConfig;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Model\System\Token;
use App\Http\Controllers\MessagePacket;

class ConfigController extends BaseController
{
    use MessagePacket;


    function  __construct(){}


    /**
     * 获取系统默认配置
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function getConfig()
    {
        try{
            $id = 1;
            $data = SystemConfig::find($id);
            return $this->success($data);

        }catch (\Exception $e){
            return $this->fail(false,'获取失败'.$e->getMessage());
        }
    }

    /**
     * 保存系统默认配置
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function setConfig(Request $request)
    {
        try{
            if(!$request->has(['defaultpassword','checkteachertime'])){
                return $this->fail(false,'参数不全');
            }
            $id = 1;
            $config = SystemConfig::find($id);
            if(empty($config)){
                ## 新建微信配置对象
                $config = new SystemConfig();
                foreach($request->all() as $key => $val){
                    $config->$key = $val;
                }

                ## 保存系统默认配置
                $config->id = $id;
                $config->created_by = Token::user()->userid;
                $config->save();
            }else{
                foreach($request->all() as $key => $val){
                    $config->$key = $val;
                }

                ## 更新系统默认配置
                $config->updated_by = Token::user()->userid;
                $config->update($request->all());
            }
            $config = SystemConfig::find($id);
            return $this->success($config,"保存成功");

        }catch (\Exception $e){
            return $this->fail(false,'保存失败'.$e->getMessage());
        }

    }

    /**
     * 获取邮箱配置
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function getMailConfig()
    {
        try{
            $id = 1;
            $data = MailConfig::find($id);
            return $this->success($data);

        }catch (\Exception $e){
            return $this->fail(false,'获取失败'.$e->getMessage());
        }
    }

    /**
     * 保存系统默认配置
     * @return \EasyWeChat\Support\Collection
     *
     */
    public function setMailConfig(Request $request)
    {
        try{
            if(!$request->has(['mail_host','mail_uername','mail_password','mail_from_address','mail_from_name'])){
                return $this->fail(false,'参数不全');
            }
            $id = 1;
            $config = MailConfig::find($id);
            if(empty($config)){
                ## 新建邮件配置对象
                $config = new MailConfig();
                foreach($request->all() as $key => $val){
                    $config->$key = $val;
                }
//                $this->testMailConfig($config);
                ## 保存邮件配置对象
                $config->id = $id;
                $config->created_by = Token::user()->userid;
                $config->save();
            }else{
                foreach($request->all() as $key => $val){
                    $config->$key = $val;
                }
//                $this->testMailConfig($config);
                ## 更新邮件配置对象
                $config->updated_by = Token::user()->userid;
                $config->update($request->all());
            }
            $config = MailConfig::find($id);
            return $this->success($config,"保存成功");

        }catch (\Exception $e){
            return $this->fail(false,'保存失败'.$e->getMessage());
        }

    }

    protected function testMailConfig(MailConfig $config)
    {
        try{
            $to = $config->mail_from_address;
            $subject = '测试SchooIn 邮箱配置';
            $content = '测试SchooIn 邮箱配置，配置成功';
            $event = New EmailEvent($to,$subject,$content);
            event($event);
        }catch (\Exception $e){
            throw $e;
        }
    }


}
